﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class WorkClassBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IWorkClassDA workClassDA = DataAccess.CreateWorkClassDA();

        public WorkClassBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public WorkClassBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> workClassIDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<WorkClassInfo> workClassInfos = new List<WorkClassInfo>();
                WorkClassInfo workClassInfo = null;
                foreach (string id in workClassIDS)
                {
                    workClassInfo = workClassDA.GetWorkClassByID(dataBase, tran, id, this.dataBaseID[0]);
                    workClassInfos.Add(workClassInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (workClassDA.DeleteWorkClass(dataBase, tran, workClassIDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < workClassIDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<string>(dataBase, tran, workClassIDS[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "ZONE", this.dataBaseID[0]);
                        temp = temp + workClassIDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return workClassDA.GetAllWorkClass(this.dataBaseID[0]);
        }

        public object GetInfoByID(string workClassID)
        {
            return workClassDA.GetWorkClassByID(workClassID, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            WorkClassQueryEntity workClassQuery = obj as WorkClassQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(workClassQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + workClassQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(workClassQuery.WorkClassID))
            {
                sqlWhere.Add(" WORK_CLASS_ID " + workClassQuery.WorkClassID);
            }
            if (!string.IsNullOrEmpty(workClassQuery.Descr))
            {
                sqlWhere.Add(" DESCR " + workClassQuery.Descr);
            }
            if (!string.IsNullOrEmpty(workClassQuery.CreatedBy))
            {
                sqlWhere.Add(" CREATED_BY " + workClassQuery.CreatedBy);
            }
            if (!string.IsNullOrEmpty(workClassQuery.CreateDate))
            {
                sqlWhere.Add(workClassQuery.CreateDate.Replace("@CreateDate@", " CREATE_DATE "));
            }
            return workClassDA.GetWorkClassByQueryList(sqlWhere, workClassQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            string WorkClassID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                WorkClassInfo workClass = obj as WorkClassInfo;
                if (workClassDA.CheckWorkClassIDUnique(dataBase, tran, workClass.WorkClassID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2160_001", this.pubParasInfo.UserLanguage, new List<string> { workClass.WorkClassID });
                }
                workClass.UpdatedBy = workClass.CreatedBy;
                if (workClassDA.InsertWorkClass(dataBase, tran, workClass, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<WorkClassInfo>(dataBase, tran, null, workClass, AuditAction.Insert, this.pubParasInfo.UserID, "VENDOR", this.dataBaseID);
                    WorkClassID = workClass.WorkClassID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return WorkClassID;
        }

        public string Update(object obj)
        {
            string WorkClassID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                WorkClassInfo workClass = obj as WorkClassInfo;
                WorkClassInfo oldworkClass = workClassDA.GetWorkClassByID(dataBase, tran, workClass.WorkClassID, this.dataBaseID[0]);
                if (workClassDA.UpdateWorkClass(dataBase, tran, workClass, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<WorkClassInfo>(dataBase, tran, oldworkClass, workClass, AuditAction.Update, this.pubParasInfo.UserID, "VENDOR", this.dataBaseID);
                    WorkClassID = workClass.WorkClassID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return WorkClassID;
        }
    }
}

